/**
 * Copyright (c) 2009-2012 XTEAM
 * All rights reserved.
 */
/**
 * File：FrontUtil.java
 * History:
 *         2012-6-18: Initially created, Chrise.
 */
package com.xteam.ecsystem.utils;

import java.io.IOException;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.ui.ModelMap;

import com.xteam.ecsystem.base.Constant;
import com.xteam.ecsystem.base.UserLoginBean;
import com.xteam.ecsystem.entities.BaseMemberBase;
import com.xteam.ecsystem.entities.SysRequestConfig;
import com.xteam.ecsystem.entities.SysSystemConfig;
import com.xteam.ecsystem.entities.SysWebConfig;
import com.xteam.ecsystem.system.ConfigManager;
import com.xteam.utils.UUID;

/**
 * 前台工具类。
 * @author Chrise
 */
public class FrontUtil {
	
	/**
	 * 获取模板路径。
	 * @author Chrise 2012-12-19
	 * @param request
	 * @param response
	 * @param config
	 * @return
	 * @throws IOException
	 */
	public static String getTemplatePath(HttpServletRequest request, HttpServletResponse response, ConfigManager config) 
		throws IOException {
		String requrl = request.getServletPath();
		
		String solution = FrontUtil.getSolution(config.getSystemConfig());
		String template = FrontUtil.getTemplate(config.getRequestConfig(solution + requrl));
		if (solution == null || template == null) {
			response.sendError(HttpServletResponse.SC_NOT_FOUND);
			return null;
		}
		
		return solution + template;
	}
	
	/**
	 * 设置公共数据。
	 * @author Chrise 2012-7-5
	 * @param request
	 * @param model
	 * @param cfg
	 */
	public static void setCommons(HttpServletRequest request, HttpServletResponse response, ModelMap model, SysSystemConfig sysCfg, SysWebConfig webCfg)throws IOException {
		boolean islogin = false;
		Object member = request.getSession().getAttribute(Constant.VAR_MEMBER.value());
		
		if (member != null) {
			islogin = true;
			model.put(Constant.VAR_MEMBER.value(), member);
		}else {
			model.put(Constant.VAR_MEMBERID.value(), getGuestId(request, response));
		}
		
		model.put(Constant.VAR_ISLOGIN.value(), islogin);		
		// 系统配置
		model.put(Constant.VAR_SYSCFG.value(), sysCfg);
		// 前端配置
		model.put(Constant.VAR_WEBCFG.value(), webCfg);
		
		model.put(Constant.VAR_CONTEXTPATH.value(), request.getContextPath());
		model.put(Constant.VAR_RESROOTPATH.value(), AppConfig.getResRootRelation());
		model.put(Constant.VAR_VIRTUALPATH.value(), AppConfig.getVirtualPath());
		model.put(Constant.VAR_PRODUCTPATH.value(), AppConfig.getProductImagePath());
		model.put(Constant.VAR_MSGFILEPATH.value(),AppConfig.getMessageImagePath());
		model.put(Constant.VAR_ARTICLEATTACHPATH.value(),AppConfig.getArticleAttachPath());
		model.put(Constant.VAR_SYSNAVSELECTED.value(), FrontUtil.getStringParam(request, "nav", ""));
	}
	
	/**
	 * 获取字符串参数。
	 * @author Chrise 2012-12-19
	 * @param request
	 * @param name
	 * @param def
	 * @return
	 */
	public static String getStringParam(HttpServletRequest request, String name, String def) {
		String value = request.getParameter(name);
		if (value == null) return def;
		
		return value;
	}
	
	/**
	 * 获取整型参数。
	 * @author Chrise 2012-12-19
	 * @param request
	 * @param name
	 * @param def
	 * @return
	 */
	public static int getIntegerParam(HttpServletRequest request, String name, int def) {
		String value = request.getParameter(name);
		
		try {
			if (value == null) return def;
			
			return Integer.valueOf(value);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return def;
	}
	
	/**
	 * 获取模板方案。
	 * @author Chrise 2012-6-18
	 * @param cfg
	 * @return
	 */
	private static String getSolution(SysSystemConfig cfg) {
		if (cfg == null) return null;
		
		String solution = cfg.getSscTemplate();
		if (solution == null || "".equals(solution)) return null;
		
		return solution;
	}
	
	/**
	 * 获取模板路径。
	 * @author Chrise 2012-6-18
	 * @param cfg
	 * @return
	 */
	private static String getTemplate(SysRequestConfig cfg) {
		if (cfg == null) return null;
		
		String template = cfg.getSrcTemplate();
		if (template == null || "".equals(template)) return null;
		
		return template;
	}
	
	/**
	 * 
	 * 获取用户ID并检查登录状态
	 * @author ls
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public static UserLoginBean checkUserLogin(HttpServletRequest request, HttpServletResponse response) throws IOException{
		//用户ID
		UserLoginBean ulb = new UserLoginBean();
		String userId = "";

		BaseMemberBase bmb = (BaseMemberBase) request.getSession().getAttribute(Constant.VAR_MEMBER.value());
		
		if(bmb != null){
			userId = bmb.getBmbId();
			ulb.setLogin(true);
		}else{
			ulb.setLogin(false);
			userId = getGuestId(request,response);
		}
		
		ulb.setUserId(userId);
		
		return ulb;
		
	}
	
	public static String getGuestId(HttpServletRequest request, HttpServletResponse response)throws IOException{
		String userId = "";
		Cookie cookie = CookiesUtils.getCookieByName(request, "XMECS_USERID");
		
		if(cookie!=null){
			userId = java.net.URLDecoder.decode(cookie.getValue(), "UTF-8");
		}else{
			userId = UUID.getUUID();
			cookie = new Cookie("XMECS_USERID",userId);
			cookie.setMaxAge(AppConfig.getCartMaxAge()*24*60*60);
			response.addCookie(cookie);
		}
		
		return userId;
	}
}
